package sentinel

import (
	"fmt"
	"github.com/garyburd/redigo/redis"
	"log"
	"time"
)

func Test() {
	sentinel, _ := NewSentinelClient("tcp", []string{"127.0.0.1:26379"}, nil, 250*time.Millisecond, 0, 250*time.Millisecond) // Get a sentinel client
	//c, _ := sc.DialMaster("master")
	// use c as any redis connection
	//c.Do("SET", "test", 1234)
	masterAddr, err := sentinel.QueryConfForMaster("mymaster")
	if err != nil {
		log.Println("err: ", err)
	}

	log.Println("masterAddr", masterAddr)

	sentinel.Send("SUBSCRIBE", "+switch-master")
	sentinel.Flush()

	for {
		msg, err := sentinel.Receive()
		if err != nil {
			log.Println("subscribe err: ", err)
			return
		}

		switch n := parser(msg).(type) {
		case redis.Message:
			fmt.Printf("Message: %s %s\n", n.Channel, n.Data)
		case redis.PMessage:
			fmt.Printf("PMessage: %s %s %s\n", n.Pattern, n.Channel, n.Data)
		case redis.Subscription:
			fmt.Printf("Subscription: %s %s %d\n", n.Kind, n.Channel, n.Count)
			if n.Count == 0 {
				return
			}
		case error:
			fmt.Printf("error: %v\n", n)
			return
		}

		// log.Println("reply: ", reply.(type))
		// process pushed message
	}

}
